package top.lixb.libcommon.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import top.lixb.libcommon.R;


/**
 * 仿微信密码输入
 */
public class WXInputPwdView extends LinearLayout implements PasswordView.OnPasswordInputListener, CustomerKeyboard.CustomerKeyboardClickListener {

    private PasswordView mPasswordView;
    private TextView mTvTitle;
    private CustomerKeyboard mKeyboard;
    private TextView mTvMsg;

    public TextView getTvForget() {
        return mTvForget;
    }

    private TextView mTvForget;

    public Button getmBtnCommit() {
        return mBtnCommit;
    }

    private Button mBtnCommit;
    private OnInputListener mFullListner;

    public WXInputPwdView(Context context) {
        super(context);
        initView(context);
    }

    public void setTitle(String title) {
        mTvTitle.setText(title);
    }

    public void setMsg(CharSequence msg) {
        //与IOS同步 不显示消息
        if (!TextUtils.isEmpty(msg)) {
            mTvMsg.setVisibility(VISIBLE);
            mTvMsg.setText(msg);
        }
    }

    public WXInputPwdView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);

    }

    public void clearPwd() {
        mPasswordView.deleteAll();
    }

    public String getPassword() {
        return mPasswordView.getText().toString().trim();
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_input_pwd_custom, this);
        mTvTitle = findViewById(R.id.tv_title);
        mPasswordView = findViewById(R.id.pwdview);
        mKeyboard = findViewById(R.id.ckeyboard);
        mBtnCommit = findViewById(R.id.btn_commit);
        mTvMsg = findViewById(R.id.tv_msg);
        mTvForget = findViewById(R.id.tv_forget);
        mPasswordView.setInputType(InputType.TYPE_NULL);
        mPasswordView.setOnPasswordFullListener(this);
        mKeyboard.setOnCustomerKeyboardClickListener(this);


    }

    public void setOnCloseListener(@NonNull CustomerKeyboard.OnCloseButtonClicklistener listener) {
        mKeyboard.setOnCloseButtonClickListener(listener);
    }


    public WXInputPwdView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @Override
    public void passwordFull(String password) {
        if (null != mFullListner) {
            mFullListner.onFullInput(password);
        }
    }

    @Override
    public void onInput(String input) {
        if (null != mFullListner) {
            mFullListner.onInput(input);
        }
    }

    @Override
    public void click(String number) {
        mPasswordView.addPassword(number);
    }

    @Override
    public void delete() {
        mPasswordView.deleteLastPassword();
    }

    /**
     * 清除显示的内容
     */
    public void clearContent() {
        clearPwd();
        mTvMsg.setText("");
    }

    public interface OnInputListener {
        void onFullInput(String pwd);

        void onInput(String input);
    }

    public void setOnFullInputListener(OnInputListener listener) {
        mFullListner = listener;
    }
}
